<?php
//
defined('BY_JYA') or exit('error');
cfc('core')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$busys = array('0'=>'空闲中','1'=>'已入座','2'=>'已下单','3'=>'已付款');
$return=array();
$return['setting']=$this->plugin_setting('basic');
if($_RQ['op']=='list'){
	if($_RQ['store_id']){
		$store_id = intval($_RQ['store_id']);
		$this->session('store_id',$store_id);
	}
	if($_RQ['order_type']){
		$order_type = intval($_RQ['order_type']);
		$this->session('order_type',$order_type);
	}
	if(!$store_id){
		$store_id=$this->session('store_id');
	}
	if(!$store_id){
		exi('请先选择门店！','error','jy_dish/index/');
	}
	$store = pdo_get('jy_dish_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$store_id));
	if($_RQ['scanning'] && $_RQ['table_id']){
		$table_id = intval($_RQ['table_id']);
		$table = pdo_get('jy_dish_table',array('uniacid'=>$_SESSION['uniacid'],'id'=>$table_id));
		if($table['status'] <1){
			exi('不好意思，该餐桌未开放，请选择其他餐桌！','error','jy_dish/store/');
		}
		if($table['busy']>0 && empty($store['in_add'])){
			exi('餐桌已被占用，请重新选择餐桌!','error','jy_dish/store/');
		}
		$this->session('table_id',$table_id);
		pdo_update('jy_dish_table',array('scanningnum +'=>1,'busy'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$table_id));
	}
	$categories = pdo_getall('jy_dish_goods_group',array('status'=>1,'uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id),'','','display desc');
	$carts=$this->session('carts');
	$allnum =0;
	if($carts){
		foreach($carts as $k=>&$v){
			$allnum += $v['num'];
		}
	}else{
		$carts=array();
	}
	foreach($categories as &$v){
		$v['goods']=pdo_getall('jy_dish_goods',array('status'=>1,'uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'group_id'=>$v['id']),'','','display desc');
		foreach($v['goods'] as &$g){
			$virtual = pdo_getall('jy_dish_order_goods',array('goods_id'=>$g['id'],'uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id),array('goods_num'));
			$g['virtual_all'] =0;
			if(!empty($virtual)){
				foreach($virtual as &$sale){
					$g['virtual_all'] +=$sale['goods_num'];
				}
			}
			$g['package']=$g['package']?json_decode($g['package'],true):array();
			if(!empty($g['package'])){
				foreach($g['package'] as $k=>&$p){
					$p['price']=$p['price']?$p['price']:$g['price'];
					$cartspackage = $carts[$g['id']]['package'];
					$p['num']=$cartspackage[$k];
					$g['cartnum'] +=$p['num'];
					$p['pprice']=$p['num']*$p['price'];
					$goodsprice += $p['num']*$p['price'];
				}	
			}else{
				$g['cartnum']= $carts[$g['id']]['num'] >0 ? $carts[$g['id']]['num'] : 0;
				$goodsprice += $g['cartnum']*$g['price'];
			}
			
		}
	}
	$allprice = $goodsprice - $youhui;
	$return['store_id']=$store_id;
	$return['categories']=$categories;
	$return['allnum']=$allnum;
	$return['allprice']=$allprice;
	$return['carts']=$carts;
	set_html('title',$store['title'].'-点餐');
	exi($return);
}
if($_RQ['op']=='diancai'){
	$goodid=intval($_RQ['goodid']);
	$num=intval($_RQ['num']);
	$packageid=intval($_RQ['packageid']);
	$carts=$this->session('carts');
	if(!$carts){
		$carts=array();
	}
	$carts[$goodid]['num']=$num;
	if($num == 0){
		unset($carts[$goodid]);
	}
	$goodinfo=pdo_get('jy_dish_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$goodid));
	$packageinfo=json_decode($goodinfo['package'],true);
	$package=$carts[$goodid]['package'];
	if(!empty($packageinfo)){
		foreach($packageinfo as $k=>$v){
			if($k == $packageid){
				if($_RQ['plus']){
					$package[$k] +=1;
				}else{
					$package[$k] -=1;
					if($package[$k] == 0){
						unset($packageinfo[$i]);
					}
				}
			}
		}
		$carts[$goodid]['package'] = $package;
	}
	$carts=$this->session('carts',$carts);
	exi($carts);
}
?>